<?php
require_once("../JavaBridge/java/Java.inc");
class reports
{	  	
			private $reportsPath="" ;
			private $reportFileName='';
			private $jasperReportsLib = "";
			private $report;
			private $javaOutputStream;
			private $fccode='';
			private $fcname=''; 
			private $year='';
			private $month='';
			private $start_date='';
			private $end_date='';
			private $bdcode='';
			private $bdname='';
			private $db;
			private $reportname='';
			private $user='';
			private $title='';
			private $code='';
			function __construct(){ 
				try {
					$CI =& get_instance();
					$this->db= $CI->load->database('default', TRUE);
					$this->reportsPath=$_SERVER["DOCUMENT_ROOT"]."/lpp/reports/";     
				} 
				catch (Exception $e) {
					echo 'Caught exception: ',  $e->getMessage(), "\n";
				}
			}
 		    function run()
 		    {
 		      try {
					// chargement des librairies au classpath 
					
			 		 
					// cr?ation de la connexion JDBC 
					$Conn = new Java("org.altic.jasperReports.JdbcConnection");
					// driver		
					$Conn->setDriver("com.microsoft.jdbc.sqlserver.SQLServerDriver");
					// url de connexion
                                        
					$Conn->setConnectString("jdbc:microsoft:sqlserver://".$this->db->hostname.":1433;DatabaseName=".$this->db->database);

					// utilisateur
					$Conn->setUser($this->db->username);
					// mot de passe
					$Conn->setPassword($this->db->password);
					 
					 
					// Compilation du fichier JRXML en fichier Jasper		
					$sJcm = new JavaClass("net.sf.jasperreports.engine.JasperCompileManager");
					//$report = $sJcm->compileReport($reportsPath .$reportFileName.".jrxml");
					$params = new Java("java.util.HashMap");
					 
					if($this->reportsPath) $params->put("SUBREPORT_DIR",$this->reportsPath);
					if($this->fccode) $params->put("P_FCCODE",$this->fccode);
					if($this->fcname)$params->put("P_FCNAME",$this->fcname);
					if($this->start_date)$params->put("P_START_DATE",$this->start_date);
					if($this->end_date) $params->put("P_END_DATE",$this->end_date);
					if($this->year) $params->put("P_YEAR",(int)$this->year);
					if($this->month) $params->put("P_MONTH",$this->month);
					if($this->bdcode) $params->put("P_BDCODE",(int)$this->bdcode);
					if($this->bdname)$params->put("P_BDNAME",$this->bdname);
					if($this->title)$params->put("P_TITLE",$this->title);
                                        if($this->code)$params->put("P_CODE",$this->code);
					if( $this->user)$params->put("P_USER", $this->user);
					// Remplir le mod?le avec les donn?es		
					$sJfm = new JavaClass("net.sf.jasperreports.engine.JasperFillManager");
					 
					$this->report = $sJfm->fillReport($this->reportsPath.$this->reportFileName.".jasper",$params ,$Conn->getConnection());
			
 					$this->javaOutputStream = new java("java.io.ByteArrayOutputStream"); 
 					
				} catch (JavaException $ex) {
					 $trace = new Java("java.io.ByteArrayOutputStream");
					$ex->printStackTrace(new Java("java.io.PrintStream", $trace));
					echo  "java stack trace: $trace\n";
				}
 		    }
		    function exportPDF()
		    {
                        $this->run();
                        set_time_limit(120);
		 
		        java_set_file_encoding("ISO-8859-1"); 
		 
		        $exporter = new java("net.sf.jasperreports.engine.export.JRPdfExporter"); 
		        $exParm = java("net.sf.jasperreports.engine.JRExporterParameter"); 
		        $exporter->setParameter($exParm->JASPER_PRINT, $this->report); 
		        $exporter->setParameter($exParm->OUTPUT_STREAM, $this->javaOutputStream); 
		        $exporter->exportReport(); 
		 
		        header('Content-Type: application/pdf'); 
		        header('Content-Transfer-Encoding: binary'); 
		        header('Content-disposition: attachment; filename="'.$this->reportname.'.pdf"');
		        header('Pragma: no-cache'); 
		        header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
		        header('Expires: 0'); 
		 
		        echo java_cast($this->javaOutputStream->toByteArray(),"S"); 		 
		    }
		    function exportExcel()
		    {
		    		$this->run();
			        set_time_limit(120); 
			 
			        java_set_file_encoding("WINDOWS-874"); 
			 		//echo "yes";
			        $exporter = new java("net.sf.jasperreports.engine.export.JRXlsExporter"); 
			        $exParm = java("net.sf.jasperreports.engine.JRExporterParameter"); 
			        $exXlsParm = java("net.sf.jasperreports.engine.export.JRXlsExporterParameter"); 
			        $exporter->setParameter($exParm->JASPER_PRINT,$this->report); 
			        $exporter->setParameter($exParm->OUTPUT_STREAM, $this->javaOutputStream); 
			        $exporter->setParameter($exXlsParm->IS_ONE_PAGE_PER_SHEET, true); 
			        $exporter->setParameter($exXlsParm->IS_DETECT_CELL_TYPE, true); 
			        $exporter->exportReport(); 
			 
			        header('Content-Type: application/xls'); 
			        header('Content-Transfer-Encoding: binary'); 
			        header('Content-disposition: attachment; filename="'.$this->reportname.'.xls"');
			        header('Pragma: no-cache'); 
			        header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
			        header('Expires: 0'); 
			 
			        echo java_cast($this->javaOutputStream->toByteArray(),"S"); 
		    }
		    function exportWord()
		    {
		    		$this->run();
			        set_time_limit(120); 
			 
			        java_set_file_encoding("ISO-8859-1"); 
			 		//echo "yes";
			        $exporter = new java("net.sf.jasperreports.engine.export.JRRtfExporter"); 
			        $exParm = java("net.sf.jasperreports.engine.JRExporterParameter"); 
			       //$exXlsParm = java("net.sf.jasperreports.engine.export.JRRtfExporterParameter"); 
			        $exporter->setParameter($exParm->JASPER_PRINT,$this->report); 
			        $exporter->setParameter($exParm->OUTPUT_STREAM, $this->javaOutputStream); 
			        //$exporter->setParameter($exXlsParm->IS_ONE_PAGE_PER_SHEET, true); 
			        //$exporter->setParameter($exXlsParm->IS_DETECT_CELL_TYPE, true); 
			        $exporter->exportReport(); 
			 
			        header('Content-Type: application/msword'); 
			        header('Content-Transfer-Encoding: binary'); 
			        header('Content-disposition: attachment; filename="'.$this->reportname.'.doc"');
			        header('Pragma: no-cache'); 
			        header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
			        header('Expires: 0'); 
			 
			        echo java_cast($this->javaOutputStream->toByteArray(),"S"); 
		    }
		    function exportHtml()
		    {
 
		    		$this->run();
		    		$sJem = new JavaClass("net.sf.jasperreports.engine.JasperExportManager");
		    		$sJem->exportReportToHtmlFile($this->report,$this->reportsPath.$this->reportFileName.".html");			
                                if (file_exists($this->reportsPath.$this->reportFileName.".html")){
                                     header('Content-disposition: attachment; filename="'.$this->reportname.'.html"');
                                     header('Content-Type: application/html');
                                     header('Content-Transfer-Encoding: binary');
                                     header('Content-Length: '. @filesize($this->reportsPath.$this->reportFileName.".html"));
                                     header('Pragma: no-cache');
                                     header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
                                     header('Expires: 0');
                                     //echo java_cast($this->javaOutputStream->toByteArray(),"S");
                                     set_time_limit(0);
                                     @readfile($this->reportsPath.$this->reportFileName.".html") or die("problem occurs.");
                                 }
		    }	
		    public function setReportFileName($file) { 
		        $this->reportFileName = $file; 
		    } 
		    public function setFCCODE($fccode) { 
		    	 
		        $this->fccode = $fccode;  
		    } 
		    public function setFCNAME($fcname) { 
		    	 
		        $this->fcname = $fcname;  
		    } 		    
			public function setYEAR($year) { 
		    	 
		        $this->year = $year;  
		    } 	
		    public function setMONTH($month) { 
		    	 
		        $this->month = $month;  
		    } 		    
		    public function setSTART_DATE($start_date) { 
		    	 
		        $this->start_date = $start_date;  
		    } 
		    public function setEND_DATE($end_date) { 
		    	 
		        $this->end_date = $end_date;  
		    }
		    public function setBDCODE($bdcode) {

		        $this->bdcode = $bdcode;
		    }
		    public function setBDNAME($bdname) {

		        $this->bdname = $bdname;
		    }
		    public function setUSER($user) {

		        $this->user = $user;
		    }
		    public function setCODE($code) {

		        $this->code = $code;
		    }
		    public function setTITLE($title) {

		        $this->title = $title;
		    }
		    public function setReportName($recode) {

		        $this->reportname = $recode;
		    }
}